// TO DO

#include "AppDelegate.h"
#include "GameLayer.h"
#include "CMyAnimPool.h"
#include "CStringRes.h"
#include "GameScene.h"

using namespace cocos2d;
using namespace CocosDenshion;

#define LINE_SPACE	40

#define MAP_ZORDER			0
#define SPRITE_BATCH_ZORDER	1
#define GAME_OBJ_ZORDER		0 // tank, bullet, explosion

GameLayer::~GameLayer(void)
{
	//m_pSpriteBatch->removeAllChildrenWithCleanup(true);
}

// on "init" you need to initialize your instance
bool GameLayer::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////
		
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
		setAccelerometerEnabled(true);
#endif // CC_PLATFORM_WIN32
		setTouchEnabled(true);

		// Get window size
        CCSize size = CCDirector::sharedDirector()->getWinSize();

		// add labeled menu

        // Add the menu to ManiMenuScene layer as a child layer.
		
		this->initGame();
		bRet = true;
    } while (0);

    return bRet;
}

void GameLayer::menuCallback(CCObject* pSender)
{
}


void GameLayer::initGame(void)
{
	// sprite batch
	//m_pSpriteBatch = CCSpriteBatchNode::create("Images/spritesheet.png");
	//this->addChild(m_pSpriteBatch, SPRITE_BATCH_ZORDER);

	for (int i = 0; i < MAX_GAME_KEYS; i++)
	{
		m_KeyInfo[i].m_bPress = false;
		m_KeyInfo[i].m_fPressTime = 0.0;
	}
	m_bKeyByAccel = false;
	
	// do update
	schedule(schedule_selector(GameLayer::updateGame));
}

void GameLayer::updateGame(float dt)
{
}

void GameLayer::didAccelerate(CCAcceleration* pAccelerationValue)
{
    double accx = pAccelerationValue->x;
    double accy = pAccelerationValue->y;
	int key = -1;

	//CCLog("didAccelerate %f, %f", accx, accy);
	for (int i = 0; i < 4; i++)
		m_KeyInfo[i].m_bPress = false;
	if (accx < 0.0) key = DIR_LEFT;
	if (accx > 0.0) key = DIR_RIGHT;
	if (accy > 0.0) key = DIR_UP;
	if (accy < 0.0) key = DIR_DOWN;
	if (key != -1)
	{
		m_KeyInfo[key].m_bPress = true;
		m_bKeyByAccel = true;
	}
}
